// require('style/css/print.scss');
// require('style/css/fixedModal.scss');

// var $ = require("expose-loader?$!jquery");
// require('common/jquery.printarea');
// var util = require('common/util');

var printBth = $('#print-btn'),
	bodyElem = $('body'),
	previewPrint = null,
	previewWrap = null,
	fixedModalMask = null,
	confirmPrint = null,
	summaryNum = 0,
	summaryPrice = 0;

/**
 * 创建订单打印预览模态框
 * @return {[type]} [description]
 */
function createPreviewModal() {

	var tem = '' +
		'<div class="modal fixed-modal hide" id="preview-print">'+
		 	'<div class = "confirm-print" id="confirm-print">打印</div>'+
		 	'<div class = "modal-content">'+
				'<div class="modal-header">'+
	            	'<i class="iconfont close-modal">&#xe63d;</i>'+
	            	// '<div class="modal-title">网点</div>'+
	        	'</div>'+
	        '<div class = "modal-body" id = "preview-wrap">'+
			'</div>'+  
		'</div>'+
		'</div><div class = "fixed-modal-mask hide" id="fixed-modal-mask">< /div>';

	if ($('#fixed-modal-mask').length <= 0) {
		tem += '<div class="fixed-modal-mask hide" id="fixed-modal-mask"></div>';
	}
	$('body').append(tem);

	setTimeout(function() {
		previewPrint = $('#preview-print');
		previewWrap = previewPrint.find('#preview-wrap');
		fixedModalMask = $('#fixed-modal-mask');
		confirmPrint = $('#confirm-print');
		previewPrint.find('.modal-content').css({
			width: 761 + 40 + 'px'
		});
	}, 40);
}

createPreviewModal();

// 显示发票预览
function showPreview() {
	previewPrint.removeClass('hide');
	fixedModalMask.removeClass('hide');
	bodyElem.addClass('body-prevent-scroll');
}

// 隐藏发票预览
function hidePreview() {
	previewPrint.addClass('hide');
	fixedModalMask.addClass('hide');
	bodyElem.removeClass('body-prevent-scroll');
}

//  打印显示loading
function showLoading() {

	var elem = $('#upload-loading-wrap');

	if (elem.length > 0) {
		elem.removeClass('hide');
	} else {
		$('body').append('<div class="upload-loading-wrap" id="upload-loading-wrap"><div class="mask"></div><img src="/static/img/loding.gif"></div>');
	}
}

//  打印隐藏loading
function hideLoading() {
	$('#upload-loading-wrap').addClass('hide');
}

// 列表打印， 获取ajax参数
function getGetParams() {
	var inputArr = $('.print-check:checked'),
		data = {
			ids: [], // 订单id
			// orderStatus: '' // 订单状态
		};
	if (inputArr && inputArr.length > 0) {
		inputArr.each(function(n, elem) { // 详情页打印
			if ($(elem).prop('checked')) {
				data.ids.push($(elem).val());
			}
		});
	}
	return data;
}

// 点击打印， 弹出打印预览
printBth.on('click', function() {
	var data = getGetParams(),
		ids = '';
		url = '/Admin/EvaluationReportReview/printReport?ids=';

	if (data.ids.length <= 0) {
		$.alert('请选择数据');
		return;
	}
	ids = data.ids.join(',');
	url += ids;

	showLoading();
	previewWrap.load(url, function(json) {
		hideLoading();
		showPreview();
	});
});

// 确认打印
bodyElem.on('click', '#confirm-print', function() {
	previewWrap.printArea.call(previewWrap);
	var data = getGetParams(),
		ids = data.ids.join(',');
	$.get('/Admin/EvaluationReportReview/ajaxUpdateHouse?ids=' + ids, function() {});
});

// 关闭发票预览
bodyElem.on('click', '.close-modal', function() {
	hidePreview();
});